/*
 * @Author: your name
 * @Date: 2020-12-30 15:37:17
 * @LastEditTime: 2021-01-08 15:07:14
 * @LastEditors: Please set LastEditors
 * @Description:逻辑处理层
 * @FilePath: \node-server\service\user.js
 */
const moment = require('moment')
const { checkLoginDao ,registerDao ,getUserInfoDao  } = require('../dao')

/**
 * @description: 获取用户信息
 * @param {*} username
 * @return {*}
 */
const getUserInfoService =async (username) => {
   const res= await getUserInfoDao(username)
       return res
}
/**
 * @description: 登录逻辑校验
 * @param {string} username
 * @param {string} password
 * @return {*}
 */ 
const checkLoginService = async (username,password)=>{
  
       const {
        status,
        userIsRegister,
    } = await checkLoginDao(username,password)//判断用户是否存在
    // 是否注册   
    if(userIsRegister) {
        // 密码是否正确
         if(status){
            return {
                status,
                msg: "登录成功"
            }
         } else {
           return {
               status,
               msg: "密码错误"
           }
         }
       }else{//用户不存在
        return {
            status,
            msg: "用户不存在"
        } 
       }
    
    
}
/**
 * @description: 用户注册
 * @param {string} username
 * @param {string} password
 * @return {*}
 */
const registerService = async (username,password)=>{
    
        moment.locale("zh-cn");
        const create_time = moment().format('YYYY年MM月D日 h:mm:ss');
        const res = await registerDao(username,password,create_time)
        if(res){
            return {
                status:1,
                msg : '注册成功',
                userId:res.insertId,
            }
        }else{
            return {
                status:0,
                msg : '用户已存在'
            }
        }
    
    
}
module.exports ={
    checkLoginService,
    registerService,
    getUserInfoService
}